﻿package {
	import alternativa.engine3d.core.Mesh;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.engine3d.materials.WireMaterial;
	import alternativa.utils.MathUtils;
	import flash.geom.Point;
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.types.Texture;

	public class Piece extends Mesh {
		
		[Embed(source="images/murs_bs.jpg")] private static const bmp0:Class;
		private static const wallTexture:Texture = new Texture(new bmp0().bitmapData, "murs_bs.jpg");
		// Dimensions de la piece
		//longueur de la piece
		protected var longueur:Number;
		//largeur de la piece
		protected var largeur:Number = 750;
		//distance entre le mur et la porte
		protected var murporte:int = 50;
		// largeur porte 
		protected var largeurporte:int = 100;
		//Hauteur de la piece
		protected var hauteur:Number = 300;
		
		//Correspond au point de départ de la création de la pièce (le plus pres de l axe)
		protected var z1:Number;
		protected var y1:Number;
		protected var x1:Number;
		
		//Décide du nombre de porte max 2
		protected var p1:Number;
		
		//Position droite ou gauche de la porte
		protected var pos:Number;	
		
		//Création des portes
		private var portesalle:Object3D;
		private var portesalle2:Object3D;
		private var cadre:Object3D;
		private var cadre2:Object3D;
		
		private var tableau:Object3D;
		
		private var document:Object3D;
		private var chaise:Object3D;
		private var bureau:Object3D;
		
		public function Piece(x:Number,y:Number,z:Number,p:Number,L:Number=1000,name:String=null)	
		{
			super(name);
			 x1 = x;
			 y1 = y;
			 z1 = z;
			 p1 = p;
			 this.longueur = L;
			 
			tableau=new Tableau();
						 
			//Création des point du rectangle du bas
			addRectangleVertices(x1, y1 , z1,longueur,largeur, "rect_bas");
			//Création des point du rectangle du haut
			addRectangleVertices(x1, y1 , z1 + hauteur ,longueur,largeur, "rect_haut");
			
			var wallsFaces:Array = [];
			var wallsFacesP:Array = [];
			var wallsFacesSP:Array = [];
			//Bas
			createFace(["rect_bas_3", "rect_bas_2" , "rect_bas_1" , "rect_bas_0"], "bas");
			//Haut
			createFace(["rect_haut_0", "rect_haut_1" , "rect_haut_2" , "rect_haut_3"], "haut");
			//"Ouest"
			wallsFaces.push(createFace([ "rect_bas_1" ,"rect_haut_1" , "rect_haut_0","rect_bas_0",  ], "wall_2"));
			//"Est"
			wallsFaces.push(createFace([ "rect_bas_3" , "rect_haut_3" , "rect_haut_2","rect_bas_2"], "wall_4"));
			
			
			
			//Face pour decider de la porte (gauche par rapport a l'axe)
			
			//Nord
			if(y1 < 0 )
			{
				tableau.x=x1;
				tableau.y=y1+200;
				tableau.z=z1+50;
				addChild(tableau);
				//Fais le mur sans la porte
				wallsFacesSP.push(createFace([ "rect_bas_0","rect_haut_0","rect_haut_3","rect_bas_3"], "wall_3"));
				
				if(p1 == 1)
				{
				addPorteVertices((murporte) + x1, y1 +largeur , z1,hauteur - murporte ,largeurporte, "porte_un");
				wallsFacesP.push(createFace(["porte_un_0","porte_un_4","rect_haut_1","rect_bas_1"], "wall_5"));
				wallsFacesP.push(createFace(["porte_un_2","porte_un_5","porte_un_4","porte_un_1"], "wall_6"));
				wallsFacesP.push(createFace(["rect_bas_2","rect_haut_2", "porte_un_5","porte_un_3" ], "wall_7"));
				portesalle = new Portesalle(0);
				portesalle.x = (murporte) + x1;
				portesalle.y = -10;
				addChild(portesalle);
				
				cadre = new Cadre(100,10,250);
				cadre.x = (murporte) + x1;
				cadre.y = -10;
				addChild(cadre);
				
				}
					else if(p1 == 2)
					{
						addPorteVertices((murporte) + x1, y1 +largeur , z1,hauteur - murporte ,largeurporte, "porte_un");
						addPorteVertices((longueur-largeurporte -murporte) + x1, y1 +largeur , z1,hauteur - murporte ,largeurporte, "porte_deux");
						wallsFacesP.push(createFace(["porte_un_0","porte_un_4","rect_haut_1","rect_bas_1"], "wall_5"));
						wallsFacesP.push(createFace(["porte_un_2","porte_un_5","porte_un_4","porte_un_1"], "wall_6"));
						wallsFacesP.push(createFace(["porte_deux_0","porte_deux_4", "porte_un_5","porte_un_3" ], "wall_7"));
						wallsFacesP.push(createFace(["porte_deux_2","porte_deux_5","porte_deux_4","porte_deux_1"], "wall_8"));
						wallsFacesP.push(createFace(["rect_bas_2","rect_haut_2","porte_deux_5","porte_deux_3"], "wall_9"));
						portesalle = new Portesalle(0);
						portesalle.x = (murporte) + x1;
						portesalle.y = -10;
						addChild(portesalle);
						portesalle2 = new Portesalle(0);
						portesalle2.x = (longueur-largeurporte -murporte) + x1;
						portesalle2.y = -10;
						addChild(portesalle2);
						cadre = new Cadre(100,10,250);
						cadre.x = (murporte) + x1;
						cadre.y = -10;
						addChild(cadre);
						cadre2 = new Cadre(100,10,250);
						cadre2.x = (longueur-largeurporte -murporte) + x1;
						cadre2.y = -10;
						addChild(cadre2);
					}
						else
						{
							wallsFacesSP.push(createFace([ "rect_bas_2" , "rect_haut_2", "rect_haut_1","rect_bas_1"], "wall_5"));
						}
			}
			//Sud
			else
			{
				tableau.rotationZ=MathUtils.toRadian(180);
				tableau.x=x1+longueur;
				tableau.y=y1+500;
				tableau.z=z1+50;
				addChild(tableau);
				
				//Fais le mur sans la porte
				wallsFacesSP.push(createFace([ "rect_bas_2" , "rect_haut_2", "rect_haut_1","rect_bas_1"], "wall_5"));
				
				//Face pour decider de la porte (droite par rapport a l'axe)
				if(p1 == 1)
				{
					addPorteVertices(murporte + x1, y1 , z1,hauteur - murporte ,largeurporte, "porte_un");
					wallsFacesP.push(createFace(["rect_bas_0","rect_haut_0","porte_un_4","porte_un_0"], "wall_3"));
					wallsFacesP.push(createFace(["porte_un_1","porte_un_4", "porte_un_5","porte_un_2"], "wall_6"));
					wallsFacesP.push(createFace(["porte_un_3", "porte_un_5","rect_haut_3","rect_bas_3"], "wall_7"));
					
					portesalle = new Portesalle(1);
					portesalle.x = (murporte) + x1;
					portesalle.y = +500;
					addChild(portesalle);
					cadre = new Cadre(100,10,250);
					cadre.x = (murporte) + x1;
					cadre.y = +500;
					addChild(cadre);
				}
					else if(p1 == 2)
					{
						addPorteVertices((longueur-largeurporte -murporte) + x1, y1 , z1,hauteur - murporte ,largeurporte, "porte_deux");
						addPorteVertices(murporte + x1, y1 , z1,hauteur - murporte ,largeurporte, "porte_un");
						wallsFacesP.push(createFace(["rect_bas_0","rect_haut_0","porte_un_4","porte_un_0"], "wall_3"));
						wallsFacesP.push(createFace(["porte_un_1","porte_un_4", "porte_un_5","porte_un_2"], "wall_6"));
						wallsFacesP.push(createFace(["porte_un_3", "porte_un_5","porte_deux_4","porte_deux_0"], "wall_7"));
						wallsFacesP.push(createFace(["porte_deux_1","porte_deux_4", "porte_deux_5","porte_deux_2"], "wall_8"));
						wallsFacesP.push(createFace(["porte_deux_3","porte_deux_5","rect_haut_3","rect_bas_3"], "wall_9"));
						portesalle = new Portesalle(1);
						portesalle.x = (murporte) + x1;
						portesalle.y = +500;
						addChild(portesalle);
						
						portesalle2 = new Portesalle(1);
						portesalle2.x = (longueur-largeurporte -murporte) + x1;
						portesalle2.y = 500;
						addChild(portesalle2);
						
						cadre = new Cadre(100,10,250);
						cadre.x = (murporte) + x1;
						cadre.y = +500;
						addChild(cadre);
						
						cadre2 = new Cadre(100,10,250);
						cadre2.x = (longueur-largeurporte -murporte) + x1;
						cadre2.y = 500;
						addChild(cadre2);
					}
						else
						{
							wallsFacesSP.push(createFace(["rect_haut_3" ,"rect_bas_3", "rect_bas_0","rect_haut_0"], "wall_3"));
						}
			}
				
					
			
			createSurface(wallsFaces, "walls");
			createSurface(wallsFacesP, "FP");
			createSurface(wallsFacesSP, "FSP");
			createSurface(["bas"], "sol");
			createSurface(["haut"], "plafond");
			setTextureMaterials();
		}
		private function addRectangleVertices(x:Number, y:Number, z:Number,L:Number,l:Number, idPrefix:String):void
		{
				createVertex(x, y, z, idPrefix + "_0");
				createVertex(x, y + l, z, idPrefix + "_1");
				createVertex(x + L,y +l, z, idPrefix + "_2");
				createVertex(x + L,y,z, idPrefix + "_3");
		}
		private function addPorteVertices(x:Number, y:Number, z:Number,h:Number,l:Number, idPrefix:String):void
		{
				createVertex(x, y, z, idPrefix + "_0");
				createVertex(x, y, z +h , idPrefix + "_1");
				createVertex(x + l,y, z  +h, idPrefix + "_2");
				createVertex(x +l,y,z, idPrefix + "_3");
				createVertex(x,y,hauteur, idPrefix + "_4");
				createVertex(x +l,y,hauteur, idPrefix + "_5");
		}
		
		protected function setTextureMaterials():void 
		{
			
			setUVsToFace(new Point(0, 0), new Point(0, 1), new Point(1, 1), "wall_4");
			setUVsToFace(new Point(0, 0), new Point(0, 1), new Point(1, 1), "wall_2");
			setMaterialToSurface(new TextureMaterial(wallTexture), "walls");
			
			setUVsToFace(new Point(0, 0), new Point(0, 1), new Point(1, 1), "wall_3");
			setUVsToFace(new Point(0, 0), new Point(0, 1), new Point(1, 1), "wall_5");
			setMaterialToSurface(new TextureMaterial(wallTexture), "FSP");
			if(p1 >= 1)
			{
				setUVsToFace(new Point(0, 0.8), new Point(0, 1), new Point(1, 1), "wall_6");
				setUVsToFace(new Point(0, 0), new Point(0, 1), new Point(2, 1), "wall_7");
			}
			if(p1 == 2)
			{	
				setUVsToFace(new Point(0, 0.8), new Point(0, 1), new Point(1, 1), "wall_8");
				setUVsToFace(new Point(0, 0), new Point(0, 1), new Point(1, 1), "wall_9");
			}
			setMaterialToSurface(new TextureMaterial(wallTexture), "FP")
			
			setUVsToFace(new Point(9.23, 0.6), new Point(9.23, 10), new Point(0, 10), "haut");
			setMaterialToSurface(new TextureMaterial(LoaderTexture.getTexture("plafond")), "plafond");
			
			setMaterialToSurface(new FillMaterial(0xCCCCCC,1,"normal",1,0xCCCCCC),"sol");
		}
        public function affichDocument():void
        {
        	document=new Document();
			document.x=x1+1380;
			document.y=y1+400;
			document.z=z1+79;
			document.rotationZ = MathUtils.toRadian(90);
			document.rotationX = MathUtils.toRadian(90);
			addChild(document);
        }
        public function addChaise():void
        {
        	if(this.y1 <0)
        	{
	        	chaise=new Chaise();
				chaise.x=x1+250;
				chaise.y=y1+500;
				chaise.z=z1;
				chaise.rotationZ = MathUtils.toRadian(90);
				addChild(chaise);
        	}
        	else
        	{
	        	chaise=new Chaise();
				chaise.x=x1+1500;
				chaise.y=y1+420;
				chaise.z=z1;
				chaise.rotationZ = MathUtils.toRadian(270);
				addChild(chaise);
        	}
        }
        public function addBureau():void
        {
        	if(this.y1 < 0)
        	{
	        	bureau=new Bureaux();
				bureau.x=x1+300;
				bureau.y=y1 +500 ;
				bureau.z=z1;
				bureau.rotationZ = MathUtils.toRadian(90);			
				addChild(bureau);
        	}
        	else
        	{
        		bureau=new Bureaux();
				bureau.x=x1+1400;
				bureau.y=y1 +400 ;
				bureau.z=z1;
				bureau.rotationZ = MathUtils.toRadian(90);			
				addChild(bureau);
        	
        	}
        }
	}
}
